﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kang.ExtendMethod
{
    public static class IntExtendMethod
    {
        private static DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);

        /// <summary>
        /// 安全方法，根据数字值，执行循环次数，默认i：0，步长：1
        /// </summary>
        /// <param name="count">循环次数</param>
        /// <param name="i">循环索引</param>
        /// <param name="j">循环索引步长</param>
        public static void For(this int count, Action<int> action, int i = 0, int j = 1)
        {
            if (action == null)
            {
                return;
            }
            if (count <= 0)
            {
                return;
            }
            for (; i < count; i = i + j)
            {
                action.Invoke(i);
            }
        }

        /// <summary>
        /// 安全方法，是否为有效的端口，此方法并不检查端口是否被占用
        /// </summary>
        /// <param name="port">端口</param>
        public static bool IsValidPort(this int port)
        {
            // 有效端口是0～65535
            return port >= 0 && port <= 65535;
        }

        /// <summary>
        /// 判断是否为真，如果为不为0和null则返回True，反之，返回False
        /// </summary>
        /// <param name="port">端口</param>
        public static bool IsTrue(this int? number)
        {
            if (number == null)
            {
                return false;
            }
            return IsTrue(number.Value);
        }

        /// <summary>
        /// 判断是否为真，如果为不为0则返回True，反之，返回False
        /// </summary>
        /// <param name="port">端口</param>
        public static bool IsTrue(this int number)
        {
            if (number == 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 判断是否为假，如果为为0和null则返回True，反之，返回False
        /// </summary>
        /// <param name="port">端口</param>
        public static bool IsFalse(this int? number)
        {
            if (number == null)
            {
                return true;
            }
            return IsFalse(number.Value);
        }

        /// <summary>
        /// 判断是否为假，如果为为0则返回True，反之，返回False
        /// </summary>
        /// <param name="port">端口</param>
        public static bool IsFalse(this int number)
        {
            if (number == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 由10位时间戳(秒)转换为DateTime类型
        /// </summary>
        /// <returns></returns>
        public static DateTime? TimeStampToDateTime(this int timeStamp)
        {
            if (timeStamp < 0)
            {
                return null;
            }
            try
            {
                return UnixEpoch.AddSeconds(timeStamp + 8 * 60 * 60);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 由13位时间戳(毫秒)转换为DateTime类型
        /// </summary>
        /// <returns></returns>
        public static DateTime? LongTimeStampToDateTime(this int longTimeStamp)
        {
            if (longTimeStamp < 0)
            {
                return null;
            }
            try
            {
                return UnixEpoch.AddMilliseconds(longTimeStamp + 8 * 60 * 60 + 1000);
            }
            catch
            {
                return null;
            }
        }
    }
}
